package com.java.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.category.mapper.CategoryMapper;
import com.java.category.service.CategoryService;
import com.java.param.PageParam;
import com.java.param.ProductHotParam;
import com.java.pojo.Category;
import com.java.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据类别名称，查询类别对象
     * @param categoryName
     * @return
     */
    @Override
    public R byName(String categoryName) {
        //封装对象
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name",categoryName);
        //查询数据库
        Category category = categoryMapper.selectOne(queryWrapper);

        //结果封装
        if(category == null){
            log.info("category 为null,类别查询失败！");
            return R.fail("类别查询失败！");
        }
        return R.ok("类别查询成功！",category);
    }

    @Override
    public R hotsCategory(ProductHotParam productHotParam) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_name",productHotParam.getCategoryName());//查集合
        queryWrapper.select("category_id");//声明只查询哪一个列、
        List<Object> ids = categoryMapper.selectObjs(queryWrapper);
        return R.ok("类型集合查询成功！",ids);
    }

    /**
     * 返回全部类型信息
     *
     * @return
     */
    @Override
    public R list() {
        List<Category> categoryList = categoryMapper.selectList(null);
        R ok = R.ok("全部类型查询成功！", categoryList);
        log.info("CategoryServiceImpl.list 业务已经完成,结果:{}", ok);
        return ok;
    }

    @Override
    public R list(PageParam pageParam) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("category_id");
        IPage<Category> page = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        page = categoryMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Category> records = page.getRecords();
        return R.ok("类别数据查询成功！",records,total);
    }

    @Override
    public R save(Category category) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name",category.getCategoryName());
        Category category1 = categoryMapper.selectOne(queryWrapper);
        if(category1!=null){
            return R.fail("类别名称重复，无法再次添加！");
        }
        int rows = categoryMapper.insert(category);
        if(rows==0){
            return R.fail("保存类别信息失败！");
        }
        return R.ok("保存类别信息成功！");
    }

    @Override
    public R remove(Category category) {
        int rows = categoryMapper.deleteById(category);
        if(rows==0){
            return R.fail("删除类别信息失败！");
        }
        return R.ok("删除类别信息成功！");
    }

    @Override
    public R update(Category category) {

        int rows = categoryMapper.updateById(category);
        if(rows==0){
            return R.fail("更新类别信息失败！");
        }
        return R.ok("更新类别信息失败！");
    }

    /**
     * 商品添加页面类别显示
     * @return
     */
    @Override
    public List<Category> productList() {
        List<Category> categoryList = categoryMapper.selectList(null);
        log.info("CategoryServiceImpl.productList 业务已经完成,结果:{}", categoryList);
        return categoryList;
    }
}
